import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ListIterator;
import java.util.List;
//import java.util.StringTokenizer;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;
import javax.xml.transform.stream.StreamSource;
import javax.xml.ws.BindingProvider;
import junit.framework.TestCase;

//import com.ibm.staf.*;
//import com.ibm.staf.service.*;
//import com.ibm.staf.service.STAFServiceInterfaceLevel30.RequestInfo;

import com.mu.rai.services.*;

public class STAFUnitTest extends TestCase {
	public static void main(String[] args) {
		try {
			String blah = "true";
			Boolean myBool = Boolean.valueOf(blah);
			if (myBool) {
				System.out.println("true");
			}
//			MuApi api = getApi();
//			List<AnalysisSummary> results = api.findAnalysisUsing("Faults");
//			printResults(results);
//			String klass = "com.mu.rai.services.Analysis";
//			System.out.println(Class.forName(klass));
//			api.findUsing("hello", Class.forName(klass).getSimpleName());
//			TemplateContainer tc = parseFileViaJAXBContext("/tmp/dos.xml");
//			NamedModelObject nmo = tc.getTemplates().getSyslogNotifierOrAnalysisOrDosAttack().get(0);
//			System.out.println(nmo.getClass().getSimpleName());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("dammit: " + e);
		} finally {
			System.out.println("done!");
		}
	}

	static TemplateContainer parseFileViaJAXBContext(final String fileName) throws JAXBException {
		final JAXBContext ctx = JAXBContext.newInstance(new Class<?>[]{TemplateContainer.class}, null);
		File file = new File(fileName);
		if (file.canRead()) {
			StreamSource ss = new StreamSource(file);
			final JAXBElement<TemplateContainer> unmarshalledObject = ctx.createUnmarshaller().unmarshal(ss, TemplateContainer.class);
			System.out.println(unmarshalledObject.toString());
			TemplateContainer response = unmarshalledObject.getValue();
			System.out.println(response.toString());
			return response;
		} else {
			throw new JAXBException("File read/parse failed for  " + fileName);
		}
	}
	
	static void printResults(List<AnalysisSummary> results)
	{
		if (!results.isEmpty()) {
			ListIterator<AnalysisSummary> iter = results.listIterator();
			System.out.println("found " + results.size() + " analyses:");
			while (iter.hasNext()) {
				AnalysisSummary cur = iter.next();
				AnalysisStatus status = cur.getStatus();
				System.out.println(cur.getName() + ", Status:  "
						+ status.value());
			}
		}		
	}
	
//	Reference r = new Reference();
//	r.setRef("dell-3 30net");
//	TelnetChannel tch = new TelnetChannel();
//	tch.setCapture(false);
//	tch.setHost(r);
//	tch.setName("telnet dell3");
//	tch.setPrompt("$");
//	tch.setTcpPort(25);			
//	blah.getTemplates().getSyslogNotifierOrAnalysisOrDosAttack().add(tch);

	static void setAuthorizationCredentials(final Object store) {
		if (store instanceof BindingProvider) {
			BindingProvider provider = (BindingProvider) store;
			provider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY,
					"admin");
			provider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY,
					"admin");
		}
	}
	
	static MuApi getApi() {
		try {
			MuApi store = new MuApi_Service(new URL(
					"http://10.10.5.71:8888/rai/services/mu?wsdl"), new QName(
					"http://services.rai.mu.com", "MuApi")).getMuApi();
			setAuthorizationCredentials(store);
			return store;
		} catch (MalformedURLException e) {
			throw new RuntimeException(e);
		}
	}
}
